﻿using BsForApi;
using BsForApi.Model;
using BsForApi.Model.Account;
using BsForApi.WX;
using HCDV.HCSP;
using hcrainMvc.CommonTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;

namespace hcrainMvc.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class WxBaseController : ApiController
    {
        [HttpPost]
        [ActionName("Login")]
        public ApiOut<LoginOut> Login(Loginin input)
        {
            var re = WxbaseApiClass.getWxOpenId(input.WxCode);
            if (re.Contains("session_key"))
            {
                Backjscode2session b = JsonConvert.DeserializeObject<Backjscode2session>(re);
                return BsForApi.Account.Login(input, b.openid);
            }
            else
            {
                var error = new ApiOut<LoginOut>();
                error.SetError(re.Replace("\"", "").Replace("{", "").Replace("}", ""));
                return error;
            }



            //return new LoginOutput { State=0, Desc="test" };
        }
        [HttpGet]
        [ActionName("LoginByKey")]
        public ApiOut<LoginOut> LoginByKey(string loginKey)
        {

            return Account.getUserInfoByKey(loginKey);

        }
        [HttpGet]
        [ActionName("Getlogininfo")]
        public string Getlogininfo()
        {
            using (dbsimple db = new dbsimple())
            {
                var dt = db.Query(@"select *from (select DATE_FORMAT(table_sp.addtime,'%m/%d') daytime ,count(1) times from table_sp
 where  type in(2015103001,20151030)    and addtime>date_add(now(), interval -774 day)  
group by DATE_FORMAT(table_sp.addtime,'%m/%d')   order by daytime desc    ) as t  order by t.daytime  LIMIT 0,14");
               return  JsonConvert.SerializeObject(dt);
            }
           

        }

        public string getWxOpenId(string code)
        {
            return WxbaseApiClass.getWxOpenId(code);
        }

        // GET api/wxbase
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/wxbase/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/wxbase
        public void Post([FromBody]string value)
        {
        }

        // PUT api/wxbase/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/wxbase/5
        public void Delete(int id)
        {
        }
    }
}
